import {
	$http
} from '@escook/request-miniprogram'

import store from '@/store/index.js'

import {
	baseUrl
} from './baseUrl.js'

import login from './login.js'

const http = $http
http.baseUrl = baseUrl
// 请求开始之前做一些事情
http.beforeRequest = function(options) {
	// console.log('请求拦截',options);

	options.header = {
			// 设置请求头 否则后端无法接收到参数
			'content-type': 'application/x-www-form-urlencoded',
			// 携带token
			token: store.getters.token
		},
		uni.showLoading({
			title: '数据加载中...',
		})
}

// 请求完成之后做一些事情
http.afterRequest = function(options) {
	console.log('响应拦截', options);
	const {
		code,
		data
	} = options.data
	console.log('响应拦截', options);

	if (code === '446') return login()

	return uni.hideLoading()
}
export {
	http
}
